import pyperclip, time
import requests
import json

def llm(content: str) -> str:
    # 构建请求数据
    data = [
        {"role": "system", "content": "你是文字拓展助手，需要拓展输入的文字。注意拓展的字段要根据输入有依据的输出。"},
        {"role": "user", "content": "拓展下面的类型，不要超过50字:\n" + content}
    ]

    # 发送POST请求
    response = requests.post(
        url="http://home:5000/chat",
        headers={"Content-Type": "application/json"},
        data=json.dumps(data),
    )

    result = [""]

    # 检查响应状态
    if response.status_code == 200:
        result = response.json()  # 获取JSON格式的响应数据
    else:
        print(f"请求失败，状态码: {response.status_code}")
        print(f"错误信息: {response.text}")

    return result[0]

pre = ""
while True:
    content = pyperclip.paste()
    if pre != content:
        # 分割
        lines = [i for i in content.split('\n') if i != '']
        resp_lisr = [llm(line) for line in lines]
        # llm
        resp = '\n'.join(resp_lisr)
        
        print("+", content)
        print("-", resp)
        
        pre = resp
        pyperclip.copy(resp)
        time.sleep(0.1)
